package com.hks360.car_treasure.activity;

import android.app.AlertDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.hks360.car_treasure.R;
import com.hks360.car_treasure.common.AppConfig;
import com.hks360.car_treasure.dialog.DateDialog;
import com.hks360.car_treasure.internet.request_net.constant.NetActionName;
import com.hks360.car_treasure.internet.request_net.nohttp.user_interface.HttpCallback;
import com.hks360.car_treasure.internet.request_net.nohttp.user_javabean.AlertListData;
import com.hks360.car_treasure.internet.request_net.nohttp.user_requstparam.AlertData;
import com.hks360.car_treasure.internet.request_net.nohttp.user_method.CallServerManager;
import com.hks360.car_treasure.model.TboxInfo;
import com.hks360.car_treasure.util.DateUtils;
import com.hks360.car_treasure.util.GsonUtil;
import com.hks360.library.util.CommonUtil;
import com.hks360.library.util.UIUtil;
import com.hks360.nohttp.Response;
import com.hks360.third_party_library.util.GsonService;

import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by rwq on 2016/4/29.
 */
public class AlertActivity extends BaseActivity implements HttpCallback{
    private String tboxId;
    private TextView mTitleTv;
    private ImageView mBackIv;
    private ImageView mSearchTv;
    private ListView mContentLv;
    private List<AlertListData> alertListDataList = null;


    //时间选择对话框的数据
    private LinearLayout content;
    private EditText et_date_start;
    private EditText et_date_end;
    private TextView tv_confirm;
    private TextView tv_cancel;
    private AlertDialog adialog;
    String year;
    String month;
    String day;
    String hour;
    String minute;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alert_activity);
        content = (LinearLayout) getLayoutInflater().inflate(R.layout.dialog_date_choose, null);
        Intent intent = getIntent();
        Uri uri = intent.getData();
        tboxId = uri.toString();
        AlertDialog.Builder pdBuidler = new AlertDialog.Builder(this).setView(content);
        adialog = pdBuidler.create();
        initView();
    }
    @Override
    public void initView() {
        initDialog(R.string.loading_data);
        setupView();
        initData();
        addListener();
    }
    @Override
    public void setupView() {
        mTitleTv = UIUtil.findViewById(this,R.id.alert_title);
        mBackIv = UIUtil.findViewById(this,R.id.alert_back);
        mContentLv = UIUtil.findViewById(this,R.id.alert_lv);
        mSearchTv = UIUtil.findViewById(this,R.id.alert_search);
        et_date_start = UIUtil.findViewById(content, R.id.et_start);
        et_date_end = UIUtil.findViewById(content, R.id.et_overtime);
        tv_confirm = UIUtil.findViewById(content, R.id.tv_dialog_confirm);
        tv_cancel = UIUtil.findViewById(content, R.id.tv_dialog_cancel);
    }

    @Override
    public void initData() {

    }

    @Override
    public void addListener() {
        mSearchTv.setOnClickListener(this);
        et_date_start.setOnClickListener(this);
        et_date_end.setOnClickListener(this);
        tv_confirm.setOnClickListener(this);
        tv_cancel.setOnClickListener(this);
        mBackIv.setOnClickListener(this);
    }




    @Override
    public void viewClick(View v) {
        switch (v.getId()){
            case R.id.alert_search:
                //弹出时间选择对话框
                showDatePickDialog();
                break;
            case R.id.et_start:
                getDate(et_date_start);
                break;
            case R.id.et_overtime:
                getDate(et_date_end);
                break;
            case R.id.alert_back:
                finish();
                break;
            case R.id.tv_dialog_confirm:
                String date1 = et_date_start.getText().toString().trim();
                String date2 = et_date_end.getText().toString().trim();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
                long time1 = 0;
                long time2 = 0;
                try {
                    time1 = format.parse(date1).getTime();
                    time2 = format.parse(date2).getTime();
                    if (time2 < time1) {
                        CommonUtil.showToast(this, "结束时间应该大于起始时间");
                        return;
                    }
                } catch (Exception e) {

                }
                adialog.dismiss();
                CallServerManager.PostServer(R.id.tv_dialog_confirm, GsonUtil.toJson(new AlertData(String.valueOf(time2).substring(0, 10), String.valueOf(time1).substring(0, 10), tboxId)),
                        NetActionName.GETALERTID, this);
//                addData2Lv();
                //开始请求网络，并把返回的数据写到ListView
                break;
            case R.id.tv_dialog_cancel:
                adialog.dismiss();
                break;
        }
    }

    private void addData2Lv() {
        mContentLv.setAdapter(new BaseAdapter() {
            @Override
            public int getCount() {
                return alertListDataList.size();
            }

            @Override
            public Object getItem(int i) {
                return null;
            }

            @Override
            public long getItemId(int i) {
                return 0;
            }

            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
                view = View.inflate(AlertActivity.this,R.layout.alert_listitem,null);
                TextView mAlertNameTv = UIUtil.findViewById(view,R.id.alert_name);
                TextView mAlertTimeTv = UIUtil.findViewById(view,R.id.alert_time);
                switch (alertListDataList.get(i).getAlertid()){
//                    case
                }
                return view;
            }
        });
    }

    private void showDatePickDialog() {
        adialog.show();
    }
    /**
     * 轨迹查询(先选择日期，再根据是否纠偏，发送请求)
     */
    private void getDate(final EditText tvDatetime) {
        year = new String();
        month = new String();
        day = new String();
        hour = new String();
        minute = new String();
        // 选择日期
        String[] date = new String[5];
        DisplayMetrics dm = new DisplayMetrics();
        this.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;
        int height = dm.heightPixels;

        if (year.isEmpty() && month.isEmpty() && day.isEmpty()) {
            String curDate = DateUtils.getCurrentDate();
            date = DateUtils.getYMDArray(curDate, "-");
        }

        if (date != null) {
            year = date[0];
            month = date[1];
            day = date[2];
            hour = date[3];
            minute = date[4];
        }

        DateDialog dateDiolog = new DateDialog(this, new DateDialog.PriorityListener() {
            @Override
            public void refreshPriorityUI(String sltYear, String sltMonth, String sltDay, String slthour, String sltmin, DateDialog.CallBack back) {
                year = sltYear;
                month = sltMonth;
                day = sltDay;
                hour = slthour;
                minute = sltmin;
                back.execute();
            }
        }, new DateDialog.CallBack() {

            public void execute() {
                tvDatetime.setText(year + "-" + month + "-" + day + " " + hour + ":" + minute);
            }
        }, year, month, day, hour, minute, width, height, "选择日期", 1);
        Window window = dateDiolog.getWindow();
        window.setGravity(Gravity.CENTER); // 此处可以设置dialog显示的位置
        dateDiolog.setCancelable(true);
        dateDiolog.show();
    }

    @Override
    public void onSuccess(int what, Response response) {
        try{
            alertListDataList = GsonService.parseJson(response.get().toString(), new TypeToken<List<AlertListData>>() {
            }.getType());
            Log.e("RWQ1",alertListDataList.get(0).toString() );
        }catch (Exception e){
            CommonUtil.showToast(this,"连接出错");
        }
    }

    @Override
    public void onFailed(int what) {

    }

    @Override
    public void onStart(int what) {
        mDialog.show();
    }

    @Override
    public void onFinish(int what) {
        mDialog.dismiss();
    }
}
